// __PUBLIC__/js/page/cms/article/deletecontentlist
require(['../../common'], function() {
  require(['article.deletecontentlist'])
})

define('article.deletecontentlist', [
  'jquery',
  'page/grid'
], function($, Grid) {
  'use strict';

  var $table = $('.table')
  var mygrid = new Grid($table)
  mygrid.init();

  function del(selected) {
    $.post(APP + '?app=cms&m=Article&a=drpArticle', {
      ids: selected.id.join()
    })
      .done(function(data) {
        if (data === '1') {
          mygrid.del(selected.index)
        } else {
          alert('删除失败')
        }
      })
  }

  function newdata(selected) {
    $.post(APP + '?app=cms&m=Article&a=newArticle', {
      ids: selected.id.join()
    })
      .done(function(data) {
        if (data === '1') {
          mygrid.del(selected.index)
        } else {
          alert('还原失败')
        }
      })
  }

  $table

  // 删除当前行
  .on('click', '.A_del', function(e) {
    e.preventDefault()
    if (confirm('确定删除已选择的内容')) {
      var current = mygrid.getCurrent(this)
      var selected = {
        id: [current.id],
        index: [current.index]
      }
      del(selected)
    }
  })

  // 数据还原
  .on('click', '.A_reset', function(e) {
    e.preventDefault()
    var current = mygrid.getCurrent(this)
    var selected = {
      id: [current.id],
      index: [current.index]
    }
    newdata(selected)
  })

  $(document)
    .on('click', '#searchbtn', function(e) {
      e.preventDefault()
      var cid = $('#cid').val(),
        title = $('#title').val()

        location.href = APP + '?app=cms&m=Article&a=deletecontentlist&cid=' + cid + '&title=' + title
    })

})